CFLAGS=-Wall -W -g

STDLIB=../stdlib
GTEST_DIR = ./gtest
USER_DIR = ./$(GTEST_DIR)/samples

# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR) -I$(GTEST_DIR)/include
# Flags passed to the C++ compiler.
CXXFLAGS += -g -DDEBUG

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = sample1_unittest hash_unittest zdefs_unittest qsort_unittest radixsort_unittest itoa_unittest zrena_unittest zhtable_unittest

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h
all : $(TESTS)

clean :
	rm -f $(TESTS) gtest.a gtest_main.a *.o *.exe *.stackdump
# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest-all.cc

gtest.a : gtest-all.o
	$(AR) $(ARFLAGS) $@ $^

gtest_main.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest_main.cc

gtest_main.a : gtest-all.o gtest_main.o
	$(AR) $(ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.

sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc

sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \
                     $(USER_DIR)/sample1.h $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc

sample1_unittest : sample1.o sample1_unittest.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@

mem.o:$(STDLIB)/mem.c $(STDLIB)/mem.h
	$(CC) $(CFLAGS) -c $< -o $@

mem_chunk.o: $(STDLIB)/mem_chunk.c $(STDLIB)/mem_chunk.h $(STDLIB)/mem.h
	$(CC) $(CFLAGS) -c $< -o $@

hash.o:$(STDLIB)/hash.c $(STDLIB)/mem.h $(STDLIB)/mem_chunk.c
	$(CC) $(CFLAGS) -c $< -o $@

hash_unittest.o: hash_unittest.cc $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c hash_unittest.cc

hash_unittest: hash.o hash_unittest.o mem.o mem_chunk.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@

zdefs.o:$(STDLIB)/zdefs.c $(STDLIB)/zdefs.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

zdefs_unittest.o: zdefs_unittest.cc $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c zdefs_unittest.cc

zdefs_unittest: zdefs_unittest.o zdefs.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@ -lpthread

qsort_unittest.o: qsort_unittest.cc $(STDLIB)/qsort.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c qsort_unittest.cc

qsort_unittest: qsort_unittest.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@

radixsort_unittest.o: radixsort_unittest.cc $(STDLIB)/radixsort.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c radixsort_unittest.cc

radixsort_unittest: radixsort_unittest.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@

itoa_unittest.o: itoa_unittest.cc $(STDLIB)/itoa.h $(STDLIB)/strtol.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c itoa_unittest.cc

itoa_unittest: itoa_unittest.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@

zrena.o:$(STDLIB)/zrena.cc $(STDLIB)/zrena.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

zrena_unittest.o: zrena_unittest.cc $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c zrena_unittest.cc

zrena_unittest: zrena.o zrena_unittest.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@

zhtable_unittest.o: zhtable_unittest.cc $(STDLIB)/zhtable.h
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c zhtable_unittest.cc

zhtable_unittest: zhtable_unittest.o gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $@
